<!--  Ofri Keidar 302933981 group 05 -->

<%@page import="view.useBeans.OrderData"%>
<%@page import="java.util.HashMap"%>
<%@page import="controller.CommonValues"%>
<%@page import="model.database.DBException"%>
<%@page import="view.WebCommons"%>
<%@page import="controller.EmployeeController"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="orderData" class="view.useBeans.OrderData" scope="page" />
<jsp:setProperty name="orderData" property="*" />

<html>
<head>
<link rel="stylesheet" href="../../style/myTemplate/style.css"
	type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Confirm order</title>
</head>
<body>

	<span class="customHeader2">Confirm order</span>

	<%
		// if submit button was not clicked, display order's details
		if (request.getParameter("submit") == null) {

			// extract order use-bean from request
			OrderData receivedData = (OrderData) request.getAttribute(WebCommons.ORDER_DATA);

			// extract controller from session
			EmployeeController controller = (EmployeeController) session.getAttribute(WebCommons.CONTROLLER);

			// get ordering customer's details and product's price
			Map<String, String> customerDetails = new HashMap<String, String>();
			Double price = null;
			try {

				// get data from system
				customerDetails = controller.getCustomerDetails(receivedData.getCustomerNumberValue());
				price = controller.getProductPrice(receivedData.getOrderedProductName());

			} catch (DBException e) {
				
				// move to error page
				response.sendRedirect("../../commonPages/ErrorPage.jsp");
			}
	%>
	
			<!-- 	display new order's details -->
		
			<div id="page">
				<div class="post">
		
					<div id="sidebar">
						<ul>
							<li>
								<h2>Please insert new order's details</h2>
							</li>
						</ul>
					</div>
					<div id="content">
						<form METHOD=POST ACTION="ConfirmOrder.jsp">
							<table>
								<tr >
									<td><span class="customFormValue">Ordering customer number</span></td>
									<td><INPUT TYPE=TEXT NAME=orderingCustomerNumber
										VALUE=<%=receivedData.getOrderingCustomerNumber()%>
										SIZE=<%=WebCommons.NUMBER_SIZE%> readonly="readonly" class="formROdata"></td>
								</tr>
								<tr>
									<td><span class="customFormValue">Name of product to order</span></td>
									<td><INPUT TYPE=TEXT NAME=orderedProductName
										VALUE="<%=receivedData.getOrderedProductName()%>"
										SIZE=<%=WebCommons.NAME_SIZE%> readonly="readonly" class="formROdata"></td>
								</tr>
								<tr>
									<td><span class="customFormValue">Amount of ordered items</span></td>
									<td><INPUT TYPE=TEXT NAME=amount
										VALUE=<%=receivedData.getAmount()%>
										SIZE=<%=WebCommons.NUMBER_SIZE%> readonly="readonly" class="formROdata"></td>
								</tr>
								<tr>
									<td><span class="customFormValue">Address for delivery</span></td>
									<td><INPUT TYPE=TEXT NAME=amount
										VALUE="<%=customerDetails.get(CommonValues.ADDRESS)%>"
										SIZE=<%=WebCommons.ADDRESS_SIZE%> readonly="readonly" class="formROdata"></td>
								</tr>
								<tr>
									<td><span class="customFormValue">Total sum to pay</span></td>
									<td><INPUT TYPE=TEXT NAME=amount
										VALUE=<%=receivedData.getAmountValue() * price%>
										SIZE=<%=WebCommons.NUMBER_SIZE%> readonly="readonly" class="formROdata"></td>
								</tr>
								<tr>
									<td><span class="customFormValue">Credit card to bill</span></td>
									<td><INPUT TYPE=TEXT NAME=amount
										VALUE="<%=customerDetails.get(CommonValues.PAYMENT_METHOD)%>"
										SIZE=<%=WebCommons.NUMBER_SIZE%> readonly="readonly" class="formROdata"></td>
								</tr>
							</table>
							<br> <INPUT TYPE=SUBMIT NAME="submit" VALUE="I confirm, make order" class="mySubmitButton">
						</form>
					</div>
				</div>
			</div>
		
		<%
			// get main-menu page
			String menuPage = (String) session.getAttribute(WebCommons.MAIN_MANU);
		%>
		<div id="footer">
			<span class="myLink"><A HREF="<%="../../menus/" + menuPage%>">Back to Menu</A></span>
			&nbsp; &nbsp; &nbsp; &nbsp;
			<span class="myLink"><A HREF="MakeOrder.jsp">Oops, need to change order details</A></span>
		</div>
		
		<%
		} else {

			try {

				// extract controller from session				
				EmployeeController controller = (EmployeeController) session.getAttribute(WebCommons.CONTROLLER);

				// try adding order
				boolean couldAdd = controller.addNewOrder(orderData.getOrderedProductName(), orderData.getAmountValue(),
						orderData.getCustomerNumberValue());

				// check success
				if (!couldAdd) {

					/* if reached here, then customer and product exist.
					 * therefore, if could not make order then an error has occurred-
					 * move to error page
					 */
					response.sendRedirect("../../commonPages/ErrorPage.jsp");
					
				} else {

					// success
					%>
					<span class="notify"><br>Order made successfully!</span>
					<%
				}

			} catch (DBException e) {
				
				// move to error page
				response.sendRedirect("../../commonPages/ErrorPage.jsp");
			}
			
			// return to main menu
			String menuPage = (String) session.getAttribute(WebCommons.MAIN_MANU);
			%>
			<div id="footer">
				<span class="myLink"><A HREF="<%="../../menus/" + menuPage%>">Back to Menu</A></span>
			</div>
			<%
		}
	%>
		
</body>
</html>